package com.hymjweb.demo.hymj.customer_manage.freight_agreement.ticket_price.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.ticket_price.bean.BCustomerFreightTicketPricePojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface BCustomerFreightTicketPriceMapper {

    class BCustomerFreightTicketPriceMapperProvider{
        public String queryTicketPriceNoPageList(Map param){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_freight_ticket_price t ");
                WHERE("1=1");
            }}.toString();

            if(param!=null && param.size()>0){
                String freightAgreementId = DataTypeUtil.toStringObject(param.get("freightAgreementId"));
                Long goodsTypeId = DataTypeUtil.toLong(param.get("goodsTypeId"),-1);
                Long standardTypeId = DataTypeUtil.toLong(param.get("standardTypeId"),-1);
                if(freightAgreementId!=null){
                    sql += " and t.freight_agreement_id = " + DataTypeUtil.quoteStr(freightAgreementId);
                }
                if(goodsTypeId!=null && goodsTypeId!=-1){
                    sql += " and t.goods_type_id = " + goodsTypeId;
                }
                if(standardTypeId!=null && standardTypeId!=-1){
                    sql += " and t.standard_type_id = " + standardTypeId;
                }
            }
            return sql;
        }

        public String queryFreightTicketPriceListByFreightAgreementId(String freightAgreementId){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_freight_ticket_price t ");
                WHERE("t.freight_agreement_id = #{freightAgreementId}");
            }}.toString();
            return sql;
        }

        public String queryFreightTicketPricePojoById(String id){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_freight_ticket_price t ");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String insert(BCustomerFreightTicketPricePojo pojo){
            String sql = new SQL() {{
                INSERT_INTO("b_customer_freight_ticket_price");
                VALUES("id","#{id}");
                VALUES("freight_agreement_id","#{freightAgreementId}");
                VALUES("goods_type_id","#{goodsTypeId}");
                VALUES("ticket_price","#{ticketPrice}");
                VALUES("comment","#{comment}");
            }}.toString();
            return sql;
        }

        public String delete(String id){
            String sql = new SQL() {{
                DELETE_FROM("b_customer_freight_ticket_price t");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String batchDeleteTicketPriceByAgreementFreightIds(String agreementFreightIds){
            String sql = new SQL() {{
                DELETE_FROM("b_customer_freight_ticket_price t");
                WHERE("t.freight_agreement_id in (" + agreementFreightIds + ")");
            }}.toString();
            return sql;
        }

        public String deleteByFreightAgreementId(String freightAgreementId){
            String sql = new SQL() {{
                DELETE_FROM("b_customer_freight_ticket_price t");
                WHERE("t.freight_agreement_id = #{freightAgreementId}");
            }}.toString();
            return sql;
        }

    }

    @Results(id = "bCustomerFreightTicketPriceMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "freight_agreement_id", property = "freightAgreementId"),
            @Result(column = "standard_type_id", property = "standardTypeId"),
            @Result(column = "goods_type_id", property = "goodsTypeId"),
            @Result(column = "ticket_price", property = "ticketPrice"),
            @Result(column = "comment", property = "comment")
    })
    @SelectProvider(type = BCustomerFreightTicketPriceMapperProvider.class, method = "queryFreightTicketPriceListByFreightAgreementId")
    List<BCustomerFreightTicketPricePojo> queryFreightTicketPriceListByFreightAgreementId(String freightAgreementId);

    @ResultMap("bCustomerFreightTicketPriceMapId")
    @SelectProvider(type = BCustomerFreightTicketPriceMapperProvider.class, method = "queryFreightTicketPricePojoById")
    BCustomerFreightTicketPricePojo queryFreightTicketPricePojoById(String id);

    @ResultMap("bCustomerFreightTicketPriceMapId")
    @SelectProvider(type = BCustomerFreightTicketPriceMapperProvider.class, method = "queryTicketPriceNoPageList")
    List queryTicketPriceNoPageList(Map param);

    @InsertProvider(type = BCustomerFreightTicketPriceMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BCustomerFreightTicketPricePojo pojo);

    @DeleteProvider(type = BCustomerFreightTicketPriceMapperProvider.class, method = "delete")
    int delete(String id);

    @DeleteProvider(type = BCustomerFreightTicketPriceMapperProvider.class, method = "batchDeleteTicketPriceByAgreementFreightIds")
    int batchDeleteTicketPriceByAgreementFreightIds(String agreementFreightIds);

    @DeleteProvider(type = BCustomerFreightTicketPriceMapperProvider.class, method = "deleteByFreightAgreementId")
    int deleteByFreightAgreementId(String freightAgreementId);

    @Insert(
            "<script>" +
                    "insert into b_customer_freight_ticket_price(id, freight_agreement_id,standard_type_id ,goods_type_id, ticket_price, comment) values " +
                    "<foreach collection='ticketPriceList' item='pojo' separator=','>" +
                    "(#{pojo.id},#{pojo.freightAgreementId},#{pojo.standardTypeId},#{pojo.goodsTypeId},#{pojo.ticketPrice},#{pojo.comment})" +
                    "</foreach>" +
            "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsert(@Param("ticketPriceList") List<BCustomerFreightTicketPricePojo> ticketPriceList);

}
